/*
 * @author Chris
 * @since 2023/2/8 19:33
 *
 * @brief    The project will ...
 *
 * @note     ...
 *
 * @warning  Do not to ...
 *
 * Change Logs:
 *   Date           Author       Notes
 *   2023-02-08     Chris        the first version
 *
 */
#include "8052.h"

#define _nop_() __asm nop __endasm
#define I2CDelay() _nop_();_nop_();_nop_();_nop_();

#define I2C_SCL P3_7
#define I2C_SDA P3_6

void I2CStart() {
    I2C_SCL = 1;
    I2C_SDA = 1;
    I2C_SDA = 0;
    I2CDelay();
    I2C_SCL = 0;
}

void I2CStop() {
    I2C_SCL = 0;
    I2C_SDA = 0;
    I2C_SCL = 1;
    I2CDelay()
    I2C_SDA = 1;
}

__bit I2CWrite(unsigned char dat) {
    for (unsigned char mask = 0x80; mask != 0; mask >>= 1) {
        I2C_SCL = 0;
        I2C_SDA = (dat & mask) == 0 ? 0 : 1;
        I2C_SCL = 1;
        I2CDelay();
    }

    I2C_SCL = 0;
    I2C_SDA = 1;
    I2C_SCL = 1;
    I2CDelay();
    __bit ack = I2C_SDA;
    I2C_SCL = 0;
    return ack;
}
